package com.employeemanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.employeemanage.domain.bean.TblSupplier;
import com.employeemanage.domain.resp.PageResult;
import com.employeemanage.domain.vo.SupplierAddVo;
import com.employeemanage.domain.vo.SupplierListVo;
import com.employeemanage.domain.vo.SupplierUpdateVo;
import com.employeemanage.service.TblSupplierService;
import com.employeemanage.mapper.TblSupplierMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
* @author 郭可欣
* @description 针对表【tbl_supplier(供应商表)】的数据库操作Service实现
* @createDate 2025-07-05 14:20:29
*/
@Service
public class TblSupplierServiceImpl extends ServiceImpl<TblSupplierMapper, TblSupplier>
    implements TblSupplierService{

    @Override
    public PageResult<TblSupplier> getSupplierList(SupplierListVo supplierListVo) {
        // 创建分页对象
        Page<TblSupplier> page = new Page<>(supplierListVo.getPageNum(), supplierListVo.getPageSize());
        
        // 创建查询条件
        LambdaQueryWrapper<TblSupplier> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (StringUtils.hasText(supplierListVo.getSupplierName())) {
            queryWrapper.like(TblSupplier::getName, supplierListVo.getSupplierName());
        }
        if (StringUtils.hasText(supplierListVo.getContact())) {
            queryWrapper.like(TblSupplier::getContact, supplierListVo.getContact());
        }
        if (StringUtils.hasText(supplierListVo.getAddress())) {
            queryWrapper.like(TblSupplier::getAddress, supplierListVo.getAddress());
        }
        
        // 按ID降序排序
        queryWrapper.orderByDesc(TblSupplier::getId);
        
        // 执行分页查询
        Page<TblSupplier> resultPage = this.page(page, queryWrapper);
        
        return new PageResult<>(resultPage);
    }

    @Override
    public Long addSupplier(SupplierAddVo supplierAddVo) {
        // 创建供应商对象
        TblSupplier supplier = new TblSupplier();
        supplier.setName(supplierAddVo.getName());
        supplier.setContact(supplierAddVo.getContact());
        supplier.setAddress(supplierAddVo.getAddress());
        supplier.setStatus("正常"); // 默认状态为正常
        supplier.setCreatedAt(new Date());
        supplier.setUpdatedAt(new Date());
        
        // 保存供应商
        this.save(supplier);
        
        return supplier.getId();
    }

    @Override
    public boolean updateSupplier(SupplierUpdateVo supplierUpdateVo) {
        // 检查供应商是否存在
        TblSupplier existingSupplier = this.getById(supplierUpdateVo.getId());
        if (existingSupplier == null) {
            return false;
        }
        
        // 更新供应商信息
        TblSupplier supplier = new TblSupplier();
        supplier.setId(supplierUpdateVo.getId());
        
        if (StringUtils.hasText(supplierUpdateVo.getName())) {
            supplier.setName(supplierUpdateVo.getName());
        }
        if (StringUtils.hasText(supplierUpdateVo.getContact())) {
            supplier.setContact(supplierUpdateVo.getContact());
        }
        if (StringUtils.hasText(supplierUpdateVo.getAddress())) {
            supplier.setAddress(supplierUpdateVo.getAddress());
        }
        if (StringUtils.hasText(supplierUpdateVo.getStatus())) {
            supplier.setStatus(supplierUpdateVo.getStatus());
        }
        
        supplier.setUpdatedAt(new Date());
        
        return this.updateById(supplier);
    }

    @Override
    public boolean deleteSupplier(Long id) {
        // 检查供应商是否存在
        TblSupplier supplier = this.getById(id);
        if (supplier == null) {
            return false;
        }
        
        // 删除供应商
        return this.removeById(id);
    }
}




